<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link rel="stylesheet" href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css" >
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/treegrid.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>
<div class="vui-form-top">
	<div id="formArea">
		<table class="from-table">
			<tr>
				<td class="label">
					<label>机构名称</label>
				</td>
				<td>
					<input type="text" autocomplete="off" class="input" name="name"/>
				</td>
				<td>
					<button class="button" id="query"><span>查询</span></button>
				</td>
			</tr>
		</table>

	</div>
</div>

<div style="padding: 10px 20px; ">
	<div id="table"></div>
</div>
<script>
var editArea = 	['400px', '300px'];

$(function(){

    $('#query').click(function(){
        $('#table').treegrid("reload",app.serializeForm());
    });

    $('#table').treegrid({
        url:app.BASE_API_URL+'company/find',
		root:app.getLogin().companyParentId,
        columns:[
            {field:'name',title:'机构名称',width:100},
            {field:'contact',title:'联系人',width:100,fixed:true},
            {field:'contactPhone',title:'联系电话',width:100},
            {title:'操作',width:100,formatter:function(row){
				return app.getOpt(row.id,'添加下级','warning')+app.getOpt(row.id,'编辑','edit')+app.getOpt(row.id,'删除','del');
            }}
        ],
		loadSuccess:loadDataSuccess
    });
});

function loadDataSuccess(){
    $('.vui-opt-del').click(function(){

        var row = $(this).parent().parent().parent().data();
        if(row.children){
            app.message(layer,"请先删除下级公司",2);
            return;
		}

        layer.confirm('确定要删除【'+row.name+'】吗？', {icon: 3, title:'提示'}, function(index){
            app.request({
                url:'company/delete',
                data:{id:row.id},
                success:function (resp) {
                    if( resp.result==0 ){
                        layer.close(index);
                        app.message(layer,'操作成功');
                        $("#table").treegrid("reload");
                    }else{
                        app.message(layer,'操作失败【'+app.getErrorMsg(resp)+'】',2);
                    }
                }
            });
        });

	});

    $('.vui-opt-edit').click(function(){
        var row = $(this).parent().parent().parent().data();
        if(row.children){
            row.children = [];
		}
        app.window(layer,{
            title:'机构编辑【'+row.name+'】',
            area: editArea,
            content: 'company_add.html?id='+row.id+'&data='+encodeURIComponent(JSON.stringify(row))
        });
    });

    $('.vui-opt-warning').click(function(){
        var row = $(this).parent().parent().parent().data();
        if(row.children){
            row.children = [];
        }
        app.window(layer,{
            title:'添加下级【'+row.name+'】',
            area: editArea,
            content: 'company_add.html?parentId='+row.id+'&data='+encodeURIComponent(JSON.stringify(row))
        });
    });
}
</script>

</body>
</html>